Drupal7 Theme 详解 (未完)

theme('theme_name');

definition element

render element order

start=>start: theme($element)
preprocess: theme_preprocess_{$element}
process: theme_process_${element}
render=>operation: function_exists(${module}_${element})
cond=>condition: Exists or No?
render_function: theme_${element}
render_template: render file://${theme}/${element}.tpl.php
end=>end
start->preprocess->process->render->cond
cond(yes)->render_function->end
cond(no)->render_template->end

preprocess

${module}_preprocess_theme_name(&$variables);

process

${module}_process_theme_name(&$variables);

${module}_${element}

/**
* 返回 html
*
* @see
* - theme_link()
* - theme_table()
*
* @param array $variables
* An associative array containing:
* - element Structured array data for ${theme_name}
* @return string
* The constructed HTML.
*
* @example
* - bootstrap_menu_tree()
* - bootstrap_table()
*/
${module}_${element}($variables);

element example

theme(‘html’)

file://${theme_path}/html.tpl.php

theme(‘page’)

file://${theme_path}/page.tpl.php

theme(‘region’) =>

theme(‘block’)

theme(‘menu_tree’)

  • @see theme(‘menu_link’) for $variables[‘tree’]
/**
* @see ${module}_${element}()
*/
function ${module}_menu_tree($variables){
return '<ul class="menu nav">' . $variables['tree'] . '</ul>';
}
function ${module}_menu_tree__main_menu($variables){
return '<ul class="menu nav main-menu">' . $variables['tree'] . '</ul>';
}
function ${module}_menu_tree__primary($variables){
return '<ul class="menu nav primary">' . $variables['tree'] . '</ul>';
}

theme(‘menu_link’)

  • @see theme(‘menu_tree’) for children => $variables[‘element’][‘#below’]
function ${module}_menu_link($variables){
$element = $variables['element'];
$sub_menu = '';
if ($element['#below']) {
/**
* render children.
* @see theme('menu_tree')
* - $element['#below']['#theme_wrappers'] 对应 menu_tree 的
*/
$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
function ${module}_menu_link__main_menu($variables){
$output = '';
//$output build...
return $output;
}